package com.qzy.reggie.controller;

import com.qzy.reggie.common.PageResult;
import com.qzy.reggie.common.Result;
import com.qzy.reggie.pojo.Category;
import com.qzy.reggie.service.LppCategoryService;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/category")
public class LppCategoryController {

    @Autowired
    private LppCategoryService categoryService;

    @GetMapping()
    public Result list(Integer page,Integer pageSize){
        PageResult pageResult = categoryService.queryCategoryList(page,pageSize);

        return Result.success("查询成功",pageResult);
    }



    @PostMapping("/logout")
    public Result logout(HttpSession session){

        session.removeAttribute("empId");


        return Result.success("退出成功");
    }

    @PostMapping()
    public Result save(@RequestBody Category category,HttpSession session){
        Long empId = (Long) session.getAttribute("empId");

        category.setCreateUser(empId);
        category.setUpdateUser(empId);
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());


        Integer ret = categoryService.save(category);

        if(ret>0){
            return Result.success("新增成功");
        }else{
            return Result.success("新增失败");
        }
    }

    @GetMapping("{id}")
    public Result getCategoryById(@PathVariable Long id){
        return Result.success(categoryService.getCategoryById(id));
    }
    public Result updateCategory(@RequestBody Category category,HttpSession session){
        Long empId = (Long) session.getAttribute("empId");
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(empId);

        Integer ret = categoryService.update(category);

        if(ret > 0){
            return Result.success("修改成功");

        }else {
            return Result.error("修改失败");
        }
    }
}
